package com.honeycomb.db.dao.hibernate.impl;

import java.util.ArrayList;
import java.util.List;

import com.honeycomb.db.dao.BaseHibernateDao;
import com.honeycomb.db.dao.hibernate.IArticleinfoDao;
import com.honeycomb.db.entity.ArticleInfo;

public class ArticleinfoDaoImpl extends BaseHibernateDao<ArticleInfo, String> implements IArticleinfoDao {

	@Override
	public List<ArticleInfo> findArticleByTags(String[] tagArr) {
		List<ArticleInfo> articleList = new ArrayList<ArticleInfo>();
		StringBuffer tagSB = new StringBuffer();
		StringBuffer sqlSB = new StringBuffer();
		try {
			tagSB.append("(");
			for(int i=0;i<tagArr.length;i++) {
				if(i != (tagArr.length-1)) {
					tagSB.append("'"+tagArr[i]+"'"+",");
				}else {
					tagSB.append("'"+tagArr[i]+"'"+")");
				}
			}
			sqlSB.append("from ArticleInfo ai,TagArticleRel tar,TagInfo ti ");
			sqlSB.append("where ai.articleId=tar.id.articleId and ti.tagId=tar.id.tagId and ti.tagId in ");
			sqlSB.append(tagSB);
			//sqlSB.append("group by ai.articleId");
			String sql = sqlSB.toString();
			
			List<Object[]> list = this.getHibernateTemplate().find(sql);
			for (int i = 0; i < list.size(); i++) {
				Object[] obj = list.get(i);
				articleList.add((ArticleInfo)obj[0]);
			}
			return articleList;
		} catch (Exception e) {
			e.printStackTrace();
			return articleList;
		}
	}
	
}
